from flask import Flask, jsonify
from flask_cors import CORS

from .controller.houseImageController import house_image_bp
from .enums import ErrorCode
from .controller.upload_controller import upload_bp
from .controller.routes import bp
from.controller.user_controller import user_bp
from flask_sqlalchemy import SQLAlchemy
from .extensions import db
from app.controller.order_controller import order_bp
from app.persistence.HouseInfo import HouseInfo

def create_app():
    app = Flask(__name__)
    # 配置数据库连接
    app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:%40Lluo1314520@localhost/house__source'
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    app.config.from_object('app.config')
    db.init_app(app)
   
    app.register_blueprint(bp)
    app.register_blueprint(upload_bp)
    app.register_blueprint(user_bp)
    app.register_blueprint(order_bp)
    app.register_blueprint(house_image_bp)
    return app

    @app.errorhandler(Exception)
    def handle_exception(e):
        return jsonify({
            "code": ErrorCode.SERVER_ERROR.code,
            "msg": ErrorCode.SERVER_ERROR.msg,
            "error": str(e)
        }), 500
